<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="../Jquery/jquery-1.8.3.min.js"></script>

<?php
function get_region_group($province,$city,$area,$prefix=''){
	$doc=new DOMDocument();
	if(!$doc->load('../region-cn-min.xml')) exit('地区XML文件不存在');
		
	$nodeP=null;
	$nodeC=null;
	$selected='';
	$v='';
	// 省份
	$return='<select name="'.$prefix.'Province">';
	$provinces=$doc->getElementsByTagName('province');
	foreach($provinces as $a){
		$v=$a->getAttribute('name');
		$selected='';
		if($province && $province==$v){
			$selected=' selected="selected"';
			$nodeP=$a;
		}
		$return.='<option value="'.$v.'"'.$selected.'>'.$v.'</option>';
	}
	$return.='</select>';
	// 城市
	if($nodeP==null) $nodeP=$provinces[0];
	$return.='<select name="'.$prefix.'City">';
	foreach($nodeP->childNodes as $a){
		$v=$a->getAttribute('name');
		$selected='';
		if($city && $city==$v){
			$selected=' selected="selected"';
			$nodeC=$a;
		}
		$return.='<option value="'.$v.'"'.$selected.'>'.$v.'</option>';
	}
	$return.='</select>';
	// 地区
	if($nodeC==null) $nodeC=$nodeP->childNodes[0];
	$return.='<select name="'.$prefix.'City">';
	foreach($nodeC->childNodes as $a){
		$v=$a->getAttribute('name');
		$selected='';
		if($area && $area==$v){
			$selected=' selected="selected"';
		}
		$return.='<option value="'.$v.'"'.$selected.'>'.$v.'</option>';
	}
	$return.='</select>';
	return $return;
}
?>
</head>

<body>
<select name="Province"></select><select name="City"></select><select name="Area"></select><br />
<select name="Province2"></select><select name="City2"></select><select name="Area2"></select>
<script type="text/javascript">
setRegion('Province','../../Api/region.php','福建省','厦门市','思明区');
setRegion('Province2','../../Api/region.php','辽宁省','大连市','中山区');
/**
* 设置省市区 2013-5-22 by sutroon
* 例如：
* setRegion('Province','../../Api/region.php','福建省','厦门市','思明区');
* setRegion('Province2','../../Api/region.php','辽宁省','大连市','中山区');
*/
function setRegion(name,uri,valp,valc,vala){
	$('select[name="'+name+'"]').load(uri,{field:'province',value:valp},function(){ 
		var $province=$(this);
		$province.change(function(){
			province_change($province,uri);
		});
		$province.next().load(uri,{field:'city',value:valc,province:$province.val()},function(){
			var $city=$(this);
			$city.change(function(){	
				city_change($city,uri);
			});
			$city.next().load(uri,{field:'area',value:vala,province:$province.val(),city:$city.val()});
		});
	});
}

// 基础函数
function province_change($o,uri){
		$c=$o.next();
		$a=$o.next().next();
		$c.load(uri,{field:'city',province:$o.val()},function(){
			$a.load(uri,{field:'area',province:$o.val(),city:$c.val()});	
		});
}
function city_change($o,uri){
		$a=$o.next();
		$p=$o.prev();
		$a.load(uri,{field:'area',province:$p.val(),city:$o.val()});
}
</script>
</body>
</html>